from time import sleep

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.edge.options import Options
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.wait import WebDriverWait

options = Options()
options.add_argument('--no-sandbox')
options.add_experimental_option("detach", True)

driver = webdriver.Edge(options=options)

driver.maximize_window()

driver.get("http://course.recttech.cn/form-submit/")

title = driver.title
print(title)

driver.implicitly_wait(0.5)

wait = WebDriverWait(driver, 10)

## 选择元素、填充数据

# 输入框
text_box = driver.find_element(by=By.NAME, value="my-text")
text_box.send_keys("Selenium设置测试")

password = driver.find_element(by=By.NAME, value="my-password")
password.send_keys("123456")

# 文本域
textarea = driver.find_element(by=By.NAME, value="my-textarea")
textarea.send_keys("这是软件质量保证与测试的重要工具")

# 只读输入框
readonly_input = driver.find_element(by=By.NAME, value="my-readonly")
readonly_input_value = readonly_input.get_attribute("value")
print(readonly_input_value)
assert readonly_input_value == "Readonly input"

# 复选框
checkbox1 = driver.find_element(by=By.ID, value="my-check-1")
checkbox2 = driver.find_element(by=By.ID, value="my-check-2")
if checkbox1.is_selected():
    checkbox1.click()
if not checkbox2.is_selected():
    checkbox2.click()

# 单选框
radio2 = driver.find_element(by=By.ID, value="my-radio-2")
radio2.click()

# Select 选择框
select_element = driver.find_element(by=By.NAME, value="my-select")
select = Select(select_element)
select.select_by_value("2")

# 文件上传
# file_input = driver.find_element(by=By.NAME, value="my-file")
# file_input.send_keys("/Users/leon/Downloads/Test_Game_2025.zip")

# 日期选择器
target_date = "2025-10-22"
date_input = driver.find_element(by=By.NAME, value="my-date")
date_input.send_keys(target_date)

# 失去焦点
blank_area = driver.find_element(By.TAG_NAME, "body")  # 点击页面 body
blank_area.click()

# wait = WebDriverWait(driver, 5)
# date_input = driver.find_element(by=By.NAME, value="my-date")
# date_input.click()
# date_selected = wait.until(
#     expected_conditions.element_to_be_clickable((By.XPATH,"/html/body/div/div[1]/table/tbody/tr[4]/td[4]"))
# )
# date_selected.click()

# 拖拽
range_input = driver.find_element(by=By.NAME, value="my-range")
action_chains = ActionChains(driver)
action_chains.drag_and_drop_by_offset(range_input, 50, 0).perform()

# 点击提交按钮
sleep(5)
submit_button = driver.find_element(by=By.CSS_SELECTOR, value="button")
submit_button.click()

# 等待网页跳转
driver.implicitly_wait(1)

# 获取结果页面的元素
message = driver.find_element(by=By.ID, value="message")
print(message.text)

sleep(2)
driver.quit()

## TEST baidu search
# contents = driver.find_elements(by=By.TAG_NAME, value="a")
# for content in contents:
#     if "Selenium" in content.text:
#         print(content.text)
